package cn.edu.scut.sys.controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.edu.scut.sys.service.TrainExceptionService;
import cn.edu.scut.sys.utils.DataRequest;
import cn.edu.scut.sys.utils.DataResponse;
import cn.edu.scut.sys.utils.StringUtils;
import cn.edu.scut.sys.vo.TrainException;


@Controller
@RequestMapping("/trainException")
public class TrainExceptionController {

	private TrainExceptionService trainExceptionService;

	@Resource(name="trainExceptionService")
	public void setTrainExceptionService(TrainExceptionService trainExceptionService) {
		this.trainExceptionService = trainExceptionService;
	}
	
	/**
	 * 
	 * @param page
	 * @param rows
	 * @param sidx
	 * @param sord
	 * @param search
	 * @param searchField
	 * @param searchOper
	 * @param searchString
	 * @param filters
	 * @return
	 */
	@RequestMapping("/list")
	@ResponseBody
	public DataResponse<TrainException> list(@RequestParam(defaultValue="1",value="page") String page,  
            @RequestParam(defaultValue="20",value="rows") String rows,  
            @RequestParam("sidx") String sidx,  
            @RequestParam("sord") String sord,  
            @RequestParam("search") boolean search,  
            @RequestParam(required=false,value="searchField") String searchField,  
            @RequestParam(required=false,value="searchOper") String searchOper,  
            @RequestParam(required=false,value="searchString") String searchString,  
            @RequestParam(required=false,value="filters") String filters  
            ){  
		 try {  
	            DataRequest request = new DataRequest();  
	            request.setPage(StringUtils.isEmpty(page) ? 1 : Integer.valueOf(page));  
	            request.setRows(StringUtils.isEmpty(rows) ? 20 : Integer.valueOf(rows));  
	            request.setSidx(sidx);  
	            request.setSord(sord);  
	            request.setSearch(search);  
	            request.setSearchField(searchField);  
	            request.setSearchOper(searchOper);  
	            request.setSearchString(searchString);  
	            return trainExceptionService.list(request);  
	        } catch (Exception e) {  
	            e.printStackTrace();  
	        }  
	        return null;  
	}
	
	@RequestMapping("/add")
	public String add(String tips, int threshold,int compare,HttpServletRequest request){
		
		try{
			// 训练项目增加
			TrainException trainException = new TrainException();
			if (null != tips)
			trainException.setTips(tips);
			trainException.setThreshold(threshold);
			trainException.setCompare(compare);
			trainExceptionService.add(trainException);
			request.setAttribute("status", 1);
			
		}catch(Exception ex){
			request.setAttribute("status", 0);

		}
		return "/page/trainExceptionAdd";
	}
	
	
	@RequestMapping("/edit/{id}")
	public String edit(@PathVariable(value = "id") String id,HttpServletRequest request){
		if (null == id) {
			return "redirect:/page/trainExceptionList.jsp";
		}
		
		TrainException trainException=trainExceptionService.getTrainException(id);
		
		if (null == trainException) {
			return "redirect:/page/trainExceptionList.jsp";
		}
		
		request.setAttribute ("trainException", trainException);
		
		return "page/trainExceptionEdit";
	}
	
	@RequestMapping("/update")
	public String update(TrainException trainException,HttpServletRequest request){
		try{
		if (null == trainException) {
			System.out.println("trainException");
			request.setAttribute("status", 0);
			return "page/trainExceptionEdit";		
			}

		
		if (null == trainException.getThreshold()) {
			System.out.println("trainExceptionpassword");
			request.setAttribute("status", 0);
			return "page/trainExceptionEdit";		
			}
		
		if (null == trainException.getCompare()) {
			System.out.println("trainException");
			request.setAttribute("status", 0);
			return "page/trainExceptionEdit";
			}
		trainExceptionService.updateTrainException(trainException);
		request.setAttribute("status", 1);
		return "page/trainExceptionEdit";
		}
		catch(Exception ex){
			request.setAttribute("status", 0);
			return "page/trainExceptionEdit";
		}
		
	}
	
	
	@RequestMapping("/del/{id}")
	public String del(@PathVariable(value = "id") int id,HttpServletRequest request){
		trainExceptionService.delTrainException(id);
	return "redirect:/page/trainExceptionList.jsp";
	}
}
